package violencerecursion;

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        int num = input.nextInt();
        System.out.println("请输入一个字符串：");
        String str = input.next();
        if(isPalindrome(num)){
            System.out.println("这个整数是回文");
        }else{
            System.out.println("这个整数不是回文");
        }
        if(isPalindrome(str)){
            System.out.println("这个字符串是回文");
        }else{
            System.out.println("这个字符串不是回文");
        }
    }
    public static boolean isPalindrome(int num){
        if(num < 0){
            return false;
        }
        char[] arr = String.valueOf(num).toCharArray(); //将num转为char类型的字符数组
        for(int i = 0;i < arr.length / 2; i++){
            if(arr[i] != arr[arr.length - 1 - i]){
                return false;
            }
        }
        return true;
    }
    public static boolean isPalindrome(String str){
        int left = 0,right = str.length() - 1;
        while(left < right){
            if(str.charAt(left) != str.charAt(right)){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}